package com.mcptt.config.service;

import com.cloudptt.api.product.log.Log;
import com.mcptt.config.AbstractConfigFile;
import com.mcptt.config.callback.IParserCallback;
import com.mcptt.config.service.sub.Common;
import com.mcptt.config.service.sub.OnNetwork;
import com.mcptt.db.DBAdapter;
import com.mcptt.db.model.ConfigFileInfo;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;

public class Service extends AbstractConfigFile implements IServiceAttr
{
//	public static final int FileType = 4;

	public static final String FileName = "org.3gpp.mcptt.service-config";
	private static final String FilePath = "//config//";
	public static final String Name = "service-configuration-info";
	// private static final String Name_SchemaLocation = "schemaLocation";
	// private static final String Name_Xsi = "xmlns:xsi";
	// private static final String Name_Xmlns = "xmlns";
	private static final String Name_Sub = "service-configuration-params";
	private static final String Name_Domain = "domain";

	// private String schemaLocation = null;
	// private String xsi = null;
	// private String xmlns = null;

	private String domain = null;
	private Common common = null;
	private OnNetwork onNetwork = null;
	private IParserCallback parserCallback = null;


	public Service(DBAdapter dbOperator, IParserCallback parserCallback)
	{
		super(dbOperator);
		this.parserCallback = parserCallback;
		this.info = readFromDb(FileName);
		if(info != null)
		{
			parse(info);
		}
	}
//	private void initFailed(IInitCallback callback,String errStr)
//	{
//		if(callback != null)
//		{
//			callback.initialFailed(errStr);
//		}
//	}
//	private void notifyFailed(IParserCallback callback,String errStr)
//	{
//		if(callback != null)
//		{
//			callback.parserNotifyFailed(errStr);
//		}
//	}

	public void parse(ConfigFileInfo info)
	{
		if(info == null)
		{
			return;
		}
		SAXBuilder builder = new SAXBuilder();// 实锟斤拷JDOM锟斤拷锟斤拷锟斤拷
		//	File file = new File(Environment.getExternalStorageDirectory()
		//		+ FilePath, FileName);
		//	FileInputStream fis = new FileInputStream(file);
		String content = info.getContent();
		ByteArrayInputStream strIs = new  ByteArrayInputStream(content.getBytes());
		try {
			Document document = builder.build(strIs);
			Element root = document.getRootElement();// 锟斤拷酶锟斤拷诘锟�<bookstore>
			if (root == null)
			{
				return;
			}
			// Namespace ns = root.getNamespace();
			// schemaLocation = root.g.getAttributeValue(Name_SchemaLocation, ns);
			// schemaLocation = root.getAttributeValue(Name_SchemaLocation);
			// xsi = root.getAttributeValue(Name_Xsi);
			// xmlns = root.getAttributeValue(Name_Xmlns);

			List<Element> list = root.getChildren();// 锟斤拷酶锟斤拷诘锟斤拷锟接节碉拷
			for (Element elem : list)
			{
				String name = elem.getName();
				Log.debug(this.getClass(), "name = " + name);
				if (name == null || name.length() <= 0)
				{
					return;
				}
				if (name.equals(Name_Sub))
				{
					parseSub(elem);
				}
			}
		}catch (Exception e)
		{
			notifyFailed(parserCallback,FileName,"Service parse Exception = " + e);
		}
	}

	public void parseSub(Element elem) throws JDOMException
	{
		if (elem == null)
		{
			return;
		}
		domain = elem.getAttributeValue(Name_Domain);
		List<Element> list = elem.getChildren();// 锟斤拷酶锟斤拷诘锟斤拷锟接节碉拷
		for (Element subElem : list)
		{
			String name = subElem.getName();
			Log.debug(this.getClass(), "name = " + name);

			if (name == null || name.length() <= 0)
			{
				return;
			}

			if (name.equals(Common.Name))
			{
				common = new Common();
				common.parse(subElem);
			} else if (name.equals(OnNetwork.Name))
			{
				onNetwork = new OnNetwork();
				onNetwork.parse(subElem);
			}
		}
	}
	
	public boolean checkMustParamOk()
	{
    	return true;
	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("\r\n");
		strBuf.append("<").append(Name);
		// strBuf.append(" ").append(Name_Domain).append("=\"").append(domain).append("\" ");
		// strBuf.append(" ").append(Name_SchemaLocation).append("=\"").append(schemaLocation).append("\" ");
		// strBuf.append(" ").append(Name_Xsi).append("=\"").append(xsi).append("\" ");
		// strBuf.append(" ").append(Name_Xmlns).append("=\"").append(xmlns).append("\" ");
		strBuf.append(">").append("\r\n");
		strBuf.append("    ").append("<").append(Name_Sub);
		strBuf.append(" ").append(Name_Domain).append("=\"").append(domain)
		.append("\" ");
		strBuf.append(">").append("\r\n");

		strBuf.append(common);
		strBuf.append(onNetwork);

		strBuf.append("    ").append("</").append(Name_Sub).append(">")
		.append("\r\n");
		strBuf.append("</").append(Name).append(">");

		return strBuf.toString();
	}





	@Override
	public void syncToSvr()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void saveInfoToDb()
	{
		// TODO Auto-generated method stub

	}
	@Override
	public CallPriority getCallPriority() {
		// TODO Auto-generated method stub
		if(this.onNetwork != null)
		{
			return onNetwork.toCallPriority();
		}
		return null;
	}
}
